package com.hk.huakangcompany.controller;

import com.github.pagehelper.PageInfo;
import com.hk.huakangcompany.commons.security.CommonResult;
import com.hk.huakangcompany.model.ProductMenu;
import com.hk.huakangcompany.model.vo.Result;
import com.hk.huakangcompany.model.vo.ResultVO;
import com.hk.huakangcompany.service.ProductMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/7 15:21
 * @Statement: 产品菜单
 */
@RestController
@RequestMapping("/productMenu")
public class ProductMenuController {

    @Autowired
    private ProductMenuService iProductMenuService;

    @PreAuthorize("hasAuthority('productMenu:inquire')")
    @GetMapping("/findAll")
    public ResultVO findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "0")Integer limit,
            ProductMenu productMenu
    ) {
        List<ProductMenu> productMenuList = iProductMenuService.findAll(page, limit, productMenu);
        PageInfo<ProductMenu> productMenuPageInfo = new PageInfo<>(productMenuList);
        int count = iProductMenuService.count(-1,-1, productMenu);
        if (productMenuList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,productMenuPageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.nullResultVO(count,productMenuPageInfo.getList());
        return resultVO;
//        return null;
    }

    @PreAuthorize("hasAuthority('productMenu:create')")
    @PostMapping("/save")
    public Result save(@RequestBody(required = false) ProductMenu productMenu) {
        if (productMenu == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        iProductMenuService.save(productMenu);
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    @PreAuthorize("hasAuthority('productMenu:findById')")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        ProductMenu productMenu = iProductMenuService.findById(id);
        if (productMenu.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(productMenu);
        result.setObject(productMenu);
        return result;
//        return null;
    }

    @PreAuthorize("hasAuthority('productMenu:update')")
    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) ProductMenu productMenu) throws IOException {
        if (productMenu == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        iProductMenuService.update(productMenu);
        Result result = CommonResult.successResult(null);
        return result;
    }
// todo 核查排序(已完毕)
    @ApiOperation("修改排序")
    @PreAuthorize("hasAuthority('productMenu:sort')")
    @PostMapping("/updateParentId")
    public Result updateParentId(@RequestBody(required = false) ProductMenu productMenu) {
        if (productMenu.getParentId() == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        int i = iProductMenuService.updateParentId(productMenu);
        if (i == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    /**
     * 通过Id删除节点及所属子节点
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('productMenu:delete')")
    @PostMapping("/deletetProductMenu/{id}")
    public Result deletetProductMenu(@PathVariable int id) {
        /**
         * 通过Id获取所属子节点
         */
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        List<ProductMenu> productMenuList = iProductMenuService.findByParentId(ids);
        //        如果查询无结果就创建一个空集合
        if (productMenuList.isEmpty()) {
            productMenuList = new ArrayList<>();
        }
        ids = productMenuList.stream().map(ProductMenu::getId).collect(Collectors.toList());
        ids.add(id);
        iProductMenuService.deleteIds(ids);
        Result result = CommonResult.successResult(null);
        return result;
    }


}
